package states;

import gui.OpCom;
import monitor.StateVariableMonitor;
import regulators.*;
import se.lth.control.realtime.DigitalIn;
import se.lth.control.realtime.DigitalOut;

public class BallAndBeam extends StateClass implements State  {

	private int counter;

	public BallAndBeam(DigitalIn initialPosSensor, DigitalOut digitalSolenoid, BeamRegul beam,
			BallAndBeamRegul ballAndBeam, OpCom op, StateVariableMonitor stateVariableMonitor) {
		super(initialPosSensor, digitalSolenoid, beam, ballAndBeam, op, stateVariableMonitor);

	}
	double reference = 0;
	double ballPosition1;
	double ballError1;
	@Override
	public void initialExcecution() {
		counter = 0;
		op.displayStep("BallAndBeam");
		if(!ballAndBeam.isRunning()) {
			ballAndBeam.restart();
		}
		beam.shutDown();
	}

	@Override
	public void excecute() {
		ballPosition1 = ballAndBeam.controllerStatMonitor.getBallposition1();
		ballError1  = stateVariableMonitor.getBallerror1();
		if(reference <= ballPosition1 + ballError1 && reference >= ballPosition1 -ballError1) {
			counter++;
		}
	}

	@Override

	public boolean stateOfExcecution() {	
		return counter > 100 ? true:false;
	}



	@Override
	public void nextState() {

		setCurrentState(new TrackSize(initialPosSensor, digitalSolenoid, beam, ballAndBeam, op, stateVariableMonitor));

	}

	@Override
	public void suspend() {
		ballAndBeam.shutDown();
	}

	@Override
	public void resume() {
		this.initialExcecution();
	}
}
